﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChurchWebsite.Filters;
using ChurchDiary.Common;
using System.Web.Routing;
using ChurchDiary.BLL;
using ChurchDiary.TableEntity;

namespace ChurchWebsite.Controllers
{
    public class BaseController : Controller
    {
        bool IsSectionOut = false;
        protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state)
        {
            if (!requestContext.HttpContext.Request.IsAjaxRequest())
            {
                try
                {
                    string Checkname = Current.Name;
                    IsSectionOut = true;
                }
                catch (Exception Ex)
                {
                    IsSectionOut = false;
                }
                if (IsSectionOut)
                {
                    string actionName = requestContext.RouteData.Values["action"].ToString();
                    string controllerName = requestContext.RouteData.Values["controller"].ToString();
                    ViewBag.Page = new BLL_Administration().GetPage(controllerName, actionName);
                }
            }
            return base.BeginExecute(requestContext, callback, state);
        }

        protected override IActionInvoker CreateActionInvoker()
        {
            return base.CreateActionInvoker();
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (IsSectionOut==false)
            {
                filterContext.Result = new RedirectResult(Url.Action("Index", "Home", new { area = string.Empty }));
            }
            base.OnActionExecuting(filterContext);
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            string actionName = filterContext.ActionDescriptor.ActionName;
            string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            EntityNotification Notification = new EntityNotification();
            Notification.ControllerName = controllerName;
            Notification.ActionName = actionName;
            string result = new BLL_Settings().InsertNotification(Notification);
        }

        protected override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);
        }

        protected override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            base.OnResultExecuted(filterContext);
        }

        protected override void HandleUnknownAction(string actionName)
        {
            base.HandleUnknownAction(actionName);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        protected override void Execute(RequestContext requestContext)
        {
            base.Execute(requestContext);
        }

        protected override void EndExecute(IAsyncResult asyncResult)
        {
            base.EndExecute(asyncResult);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
        }

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
        }

        protected override void ExecuteCore()
        {
            base.ExecuteCore();
        }

        protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
        {
            return base.BeginExecuteCore(callback, state);
        }

        protected override ITempDataProvider CreateTempDataProvider()
        {
            return base.CreateTempDataProvider();
        }

        protected override void EndExecuteCore(IAsyncResult asyncResult)
        {
            base.EndExecuteCore(asyncResult);
        }

       
    }
}
